
/*
This file is part of flashforumlib.

flashforumlib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

flashforumlib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with flashforumlib.  If not, see <http://www.gnu.org/licenses/>.
*/

package fflib.utils
{
	import fflib.utils.ArrayUtil;
	
	/**
	 * Bietet Hilfsfunktionen zum Umgang mit String-Instanzen.
	 */
	public class StringUtil
	{
		/**
		 * Vervielfacht einen <code>String</code> um den Faktor <code>factor</code>.
		 * 
		 * @param	string der zu vervielfachende <code>String</code>
		 * @param	factor gibt an, wie oft der <code>String</code> wiederholt wird
		 * @return	<code>string</code> so oft verkettet, wie in <code>factor</code>
		 * 	angegeben
		 */
		public static function multiply(string:String, factor:int):String
		{
			var result:String = "";
			
			var i:int;
			
			for (i = 0; i < factor; i++) {
				result += string;
			}
			
			return result;
		}
		
		/**
		 * Entfernt die Whitespaces am Anfang und am Ende des Strings <code>string</code>.<br />
		 * Der übergebene String wird dabei nicht verändert.
		 * 
		 * @param string Der zu trimmende String.
		 * 
		 * @return Der gekürzte String.
		 */
		public static function trim(string: String): String {
			var str_arr: Array = string.split('');
			
			var i: int;
			for (i = 0; i < 2; i++) {
				while (str_arr[str_arr.length - 1] == ' '  ||
					   str_arr[str_arr.length - 1] == '\t' ||
					   str_arr[str_arr.length - 1] == '\n' ||
					   str_arr[str_arr.length - 1] == '\r')
					str_arr.pop();
				str_arr.reverse();
			}
			
			return ArrayUtil.toString(str_arr);
		}
	}
}
